package com.tianhui.consignortwofei.pattern.freightCalculation;

import android.text.TextUtils;

import com.fgs.common.utils.CommonUtil;
import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;

import java.math.BigDecimal;

/**
 * @author fgs
 * @date 2020/9/23
 * @description
 **/
public class FreightCalculationNew implements IFreightCalculationNew {
    //亏吨扣除
    @Override
    public String getDeductPrice(OrderInfo orderInfo) {
        //亏吨扣除 = （装车重量 - 结算重量 - 合理路损）* 货物单价（计算结果不能为负数，为负数则为0）
        BigDecimal deductPrice = new BigDecimal("0");
        String goodsUnitPrice = TextUtils.isEmpty(orderInfo.goodsunitprice) ? "0" : orderInfo.goodsunitprice;
        try {
            deductPrice = new BigDecimal(getLoseWeight(orderInfo)).multiply(new BigDecimal(goodsUnitPrice));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deductPrice.compareTo(new BigDecimal("0")) == 1 ? CommonUtil.keepTwoDecimal(deductPrice.toString()) : "0";
    }

    //合理路耗
    @Override
    public String getReasonableWeight(OrderInfo orderInfo) {
        //合理路耗
        //1.定额
        //2.定率 装车重量*路损比例

        String consume = "0";
        if (orderInfo != null) {
            if (!TextUtils.isEmpty(orderInfo.consumequota)) {//定额
//                consume = StringUtils.toFloat(orderInfo.consumequota) / 1000;
                consume = new BigDecimal(orderInfo.consumequota).divide(new BigDecimal("1000"), 4, BigDecimal.ROUND_HALF_UP).toString();
            } else if (!TextUtils.isEmpty(orderInfo.consumefixedrate) && !TextUtils.isEmpty(orderInfo.tranweight)) { //定率
//                consume = StringUtils.toFloat(orderInfo.tranweight) * StringUtils.toFloat(orderInfo.consumefixedrate) / 1000;
                consume = new BigDecimal(orderInfo.tranweight).multiply(new BigDecimal(orderInfo.consumefixedrate)).divide(new BigDecimal("1000"), 4, BigDecimal.ROUND_HALF_UP).toString();
            }
        }
        return CommonUtil.keepThreeDecimal(consume);
    }

    @Override
    public String getServicePrice(OrderInfo orderInfo, String grants, String otherPrice) {
        //(运费单价 * 结算重量 + 补助金 + 保险费用 - 亏吨扣除 - 扣除费用) * 税率/100（废弃）
        //服务费 = （（运费单价 * 结算重量 + 补助金  - 亏吨扣除 - 扣除费用）/（1 - 税率/100））-（运费单价 * 结算重量 + 补助金  - 亏吨扣除 - 扣除费用）

        grants = TextUtils.isEmpty(grants) ? "0" : grants;
        otherPrice = TextUtils.isEmpty(otherPrice) ? "0" : otherPrice;

        String price = TextUtils.isEmpty(orderInfo.tradprice) ? "0" : orderInfo.tradprice;
        String settleWeight = TextUtils.isEmpty(orderInfo.recelveweightconfirm) ? "0" : orderInfo.recelveweightconfirm;
        String handlingfee = TextUtils.isEmpty(orderInfo.handlingfee)?"0":orderInfo.handlingfee;
//        String insurePrice = TextUtils.isEmpty(orderInfo.insureprice) ? "0" : orderInfo.insureprice; //保险费用

        BigDecimal firstResult = new BigDecimal(price)
                .multiply(new BigDecimal(settleWeight))
                .add(new BigDecimal(grants))
//                .subtract(new BigDecimal(getDeductPrice(orderInfo)))
                .subtract(new BigDecimal(otherPrice));
        BigDecimal handlingFee = new BigDecimal(handlingfee).divide(new BigDecimal("100"), 3, BigDecimal.ROUND_HALF_UP);

        BigDecimal secondResult = new BigDecimal("1")
                .subtract(handlingFee);

        BigDecimal result = firstResult
//                .divide(secondResult, 2, BigDecimal.ROUND_HALF_UP)
                .divide(secondResult, 2, BigDecimal.ROUND_HALF_DOWN)
                .subtract(firstResult);

//        BigDecimal result = new BigDecimal(price)
//                .multiply(new BigDecimal(settleWeight))
//                .add(new BigDecimal(grants))
//                .add(new BigDecimal(insurePrice))
//                .subtract(new BigDecimal(getDeductPrice(orderInfo)))
//                .subtract(new BigDecimal(otherPrice))
//                .multiply(new BigDecimal(orderInfo.handlingfee))
//                .divide(new BigDecimal("100"));
        return result.compareTo(new BigDecimal("0")) == -1 ? "0" : CommonUtil.keepTwoDecimal(result.toString());
    }

    @Override
    public String getLoseWeight(OrderInfo orderInfo) {

        String weight;
        if (!TextUtils.isEmpty(orderInfo.recelveweightconfirm)) {
            weight = orderInfo.recelveweightconfirm;
        } else if (!TextUtils.isEmpty(orderInfo.tranweightconfirm)) {
            weight = orderInfo.tranweightconfirm;
        } else if (!TextUtils.isEmpty(orderInfo.recelveweight)) {
            weight = orderInfo.recelveweight;
        } else {
            weight = "0";
        }
        String tranWeight = TextUtils.isEmpty(orderInfo.tranweight) ? "0" : orderInfo.tranweight;
        // 装车重量-确认收货重量-合理损路耗
        BigDecimal loseWeight = new BigDecimal(tranWeight)
                .subtract(new BigDecimal(weight))
                .subtract(new BigDecimal(getReasonableWeight(orderInfo)));

        return loseWeight.compareTo(new BigDecimal("0")) == 1 ? CommonUtil.keepThreeDecimal(loseWeight.toString()) : "0";

//        double loseWeightResult = loseWeight.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
//        if (loseWeightResult < 0) {
//            return "0";
//        } else {
//            return loseWeightResult + "";
//        }

    }

    @Override
    public String getDriverOtherPrice(OrderInfo orderInfo) {
        String otherExpenses;
        LogUtil.print("getDriverOtherPrice---belongBank", orderInfo.belongbank);
        if (!TextUtils.isEmpty(orderInfo.belongbank) && orderInfo.belongbank.equals("2")) {
            otherExpenses = TextUtils.isEmpty(orderInfo.driverotherpirce) ? "0" : orderInfo.driverotherpirce;
        } else {
            otherExpenses = TextUtils.isEmpty(orderInfo.driverotherpirce) ? "0" : orderInfo.driverotherpirce;
        }
        return CommonUtil.keepTwoDecimal(otherExpenses);
    }

    @Override
    public String getDriverPrice(OrderInfo orderInfo, String goodsPrice, String price, String grants, String otherPrice, String zhuangChePrice, String cangChuPrice) {
        // 公式：运费单价 x 结算重量 + 补助金 - 亏吨扣除 - 扣除费用 - 其他费用
        String settleWeight = TextUtils.isEmpty(orderInfo.recelveweightconfirm) ? "0" : orderInfo.recelveweightconfirm;
        String deductPrice = getDeductPrice(orderInfo);
        String otherExpenses = getDriverOtherPrice(orderInfo);

        price = TextUtils.isEmpty(price) ? "0" : price;
        grants = TextUtils.isEmpty(grants) ? "0" : grants;
        otherPrice = TextUtils.isEmpty(otherPrice) ? "0" : otherPrice;

        zhuangChePrice = TextUtils.isEmpty(zhuangChePrice) ? "0" : zhuangChePrice;
        cangChuPrice = TextUtils.isEmpty(cangChuPrice) ? "0" : cangChuPrice;

        BigDecimal result = new BigDecimal(price)
                .multiply(new BigDecimal(settleWeight))
                .add(new BigDecimal(grants))
                .add(new BigDecimal(zhuangChePrice))
                .add(new BigDecimal(cangChuPrice))
//                .subtract(new BigDecimal(deductPrice))
                .subtract(new BigDecimal(otherPrice))
                .subtract(new BigDecimal(otherExpenses));
        return result.compareTo(new BigDecimal("0")) == -1 ? "0" : CommonUtil.keepTwoDecimal(result.toString());
    }

    @Override
    public String getTotalPrice(OrderInfo orderInfo, String goodsPrice, String price, String grants, String otherPrice, String zhuangChePrice, String cangChuPrice,String mServicePrice) {
        // 公式：运费单价 x 结算重量 + 补助金 + 保险费用 + 服务费用 - 亏吨扣除 - 扣除费用 + 经济人

        price = TextUtils.isEmpty(price) ? "0" : price;
        grants = TextUtils.isEmpty(grants) ? "0" : grants;
        otherPrice = TextUtils.isEmpty(otherPrice) ? "0" : otherPrice;

        zhuangChePrice = TextUtils.isEmpty(zhuangChePrice) ? "0" : zhuangChePrice;
        cangChuPrice = TextUtils.isEmpty(cangChuPrice) ? "0" : cangChuPrice;

        String settleWeight = TextUtils.isEmpty(orderInfo.recelveweightconfirm) ? "0" : orderInfo.recelveweightconfirm;
        String insurePrice = TextUtils.isEmpty(orderInfo.insureprice) ? "0" : orderInfo.insureprice;
        String brokerCommission = orderInfo.brokercost + "";//经济人
        String handlingPrice = getServicePrice(orderInfo, grants, otherPrice);
        String deductPrice = getDeductPrice(orderInfo);

        BigDecimal result = new BigDecimal(price)
                .multiply(new BigDecimal(settleWeight))
                .add(new BigDecimal(grants))
                .add(new BigDecimal(insurePrice))
                .add(new BigDecimal(handlingPrice))

                .add(new BigDecimal(zhuangChePrice))
                .add(new BigDecimal(cangChuPrice))

//                .subtract(new BigDecimal(deductPrice))
                .subtract(new BigDecimal(otherPrice))
                .add(new BigDecimal(brokerCommission))
                .setScale(2, BigDecimal.ROUND_HALF_DOWN)
                ;


        return result.compareTo(new BigDecimal("0")) == -1 ? "0" : CommonUtil.keepTwoDecimal(result.toString());
    }

    @Override
    public String getTotalTaxPrice(OrderInfo orderInfo, String goodsPrice, String price, String grants, String otherPrice, String zhuangChePrice, String cangChuPrice, String mServicePrice) {
        // 公式：运费单价 x 结算重量 + 补助金 + 保险费用 + 服务费用 - 亏吨扣除 - 扣除费用

        price = TextUtils.isEmpty(price) ? "0" : price;
        grants = TextUtils.isEmpty(grants) ? "0" : grants;
        otherPrice = TextUtils.isEmpty(otherPrice) ? "0" : otherPrice;

        zhuangChePrice = TextUtils.isEmpty(zhuangChePrice) ? "0" : zhuangChePrice;
        cangChuPrice = TextUtils.isEmpty(cangChuPrice) ? "0" : cangChuPrice;

        String settleWeight = TextUtils.isEmpty(orderInfo.recelveweightconfirm) ? "0" : orderInfo.recelveweightconfirm;
        String insurePrice = TextUtils.isEmpty(orderInfo.insureprice) ? "0" : orderInfo.insureprice;
        String handlingPrice = getServicePrice(orderInfo, grants, otherPrice);
        String deductPrice = getDeductPrice(orderInfo);


        BigDecimal result = new BigDecimal(price)
                .multiply(new BigDecimal(settleWeight))
                .add(new BigDecimal(grants))
                .add(new BigDecimal(insurePrice))
                .add(new BigDecimal(handlingPrice))

                .add(new BigDecimal(zhuangChePrice))
                .add(new BigDecimal(cangChuPrice))

//                .subtract(new BigDecimal(deductPrice))
                .subtract(new BigDecimal(otherPrice))
                .setScale(2, BigDecimal.ROUND_HALF_DOWN)
                ;

        return result.compareTo(new BigDecimal("0")) == -1 ? "0" : CommonUtil.keepTwoDecimal(result.toString());
    }


}
